package com.imooc.demo.common.util;

import java.io.*;
import java.util.Properties;

/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: PropertiesUtils.java
 * Author:   cakin
 * Date:     2020/8/18
 * Description: 属性文件工具类
 */
public class PropertiesUtils {
    // 默认的属性配置文件
    private String properiesName = "sysConfig.properties";

    public PropertiesUtils() {
    }

    public PropertiesUtils(String fileName) {
        this.properiesName = fileName;
    }

    /**
     * 功能描述：读取文件的键
     *
     * @param key 属性文件的键
     * @return String 键对应的值
     * @author cakin
     * @date 2020/8/18
     */
    public String readProperty(String key) {
        String value = "";
        InputStream is = null;
        try {
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream(
                    properiesName);
            Properties p = new Properties();
            p.load(is);
            value = p.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

    /**
     * 功能描述：获取Properties
     *
     * @author cakin
     * @date 2020/8/18
     */
    public Properties getProperties() {
        Properties p = new Properties();
        InputStream is = null;
        try {
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream(properiesName);
            p.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return p;
    }

    /**
     * 功能描述：将键值对写入属性文件
     *
     * @param key   属性文件的键
     * @param value 属性文件的值
     * @author cakin
     * @date 2020/8/18
     */
    public void writeProperty(String key, String value) {
        InputStream is = null;
        OutputStream os = null;
        Properties p = new Properties();
        try {
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream(properiesName);
            p.load(is);
            // 文件路径为类路径：E:/IDEA/target/classes/sysConfig.properties
            String fileName = Thread.currentThread().getContextClassLoader().getResource(properiesName).getPath();
            os = new FileOutputStream(fileName);
            p.setProperty(key, value);
            p.store(os, key);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is)
                    is.close();
                if (null != os)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        // 测试 readProperty 功能
        test1();
        // 测试 getProperties 功能
        test2();
        // 将键值对写入文件
        test3();
    }

    /**
     * 功能描述：测试 readProperty 功能
     *
     * @author cakin
     * @date 2020/8/18
     */
    public static void test1() {
        String isSendStr = new PropertiesUtils().readProperty("is_send_message");
        if (isSendStr != null && !isSendStr.isEmpty()) {
            System.out.println(isSendStr);
        }
    }

    /**
     * 功能描述：测试getProperties
     *
     * @author cakin
     * @date 2020/8/18
     */
    public static void test2() {
        PropertiesUtils propertiesUtils = new PropertiesUtils();
        Properties properties = propertiesUtils.getProperties();
        String is_send_message = properties.getProperty("is_send_message");
        System.out.println(is_send_message);
    }

    /**
     * 功能描述：将键值对写入属性文件
     *
     * @author cakin
     * @date 2020/8/18
     */
    public static void test3() {
        PropertiesUtils propertiesUtils = new PropertiesUtils();
        propertiesUtils.writeProperty("cakin24","cakin24");
    }
}
